<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>搜索文本高亮</title>
  <style>
    *,
    *::after,
    *::before {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    :root {
      min-height: 100vh;
    }

    :root:focus-within {
      scroll-behavior: smooth;
    }

    body {
      min-height: inherit;
    }

    em {
      font-size: 45px;
      color: red;
    }
  </style>
</head>

<body>
  <input type="text" name="search" id="search">
  <div id="text">我是谁，啊啊啊</div>
</body>
<script>
  "use strict"; {
    let ipt = document.getElementById("search");
    let txt = document.getElementById("text");
    let text = txt.innerHTML;

    ipt.addEventListener("blur", e => {
      let key = e.target.value;
      let reg
      key && (reg = new RegExp(key, "gi"));

      if (reg) {
        text = text.replace(reg, v => {
          console.log("k ==", v);
          return `<em>${v}</em>`;
        });
        if (text) {
          txt.innerHTML = text;
        };
      };
    });
  };
</script>

</html>